package com.itheima.map;

import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class TreeMapDemo01 {
    public static void main(String[] args) {
        TreeMap<Student1,String> treeMap = new TreeMap<>(new Comparator<Student1>() {
            @Override
            public int compare(Student1 o1, Student1 o2) {
                int result = o1.getAge()-o2.getAge();
                result = result == 0 ? o1.getName().compareTo(o2.getName()) : result;
                return result;
            }
        });

        treeMap.put(new Student1("张三",23),"福建");
        treeMap.put(new Student1("李四",18),"广东");
        treeMap.put(new Student1("王五",30),"北京");

        treeMap.forEach((Student1 key , String value )->{
            System.out.println(key +"---"+value);
        });

        System.out.println("=================");

        Set<Student1> stus = treeMap.keySet();
        for (Student1 stukey : stus) {
            System.out.println(stukey+"***"+treeMap.get(stukey));
        }

        System.out.println("=================");
        Set<Map.Entry<Student1, String>> entries = treeMap.entrySet();
        for (Map.Entry<Student1, String> entry : entries) {
            System.out.println(entry.getKey()+"---"+entry.getValue());
        }

    }
}
class Student1{
    private String name;
    private Integer age;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student1 student = (Student1) o;

        if (name != null ? !name.equals(student.name) : student.name != null) return false;
        return age != null ? age.equals(student.age) : student.age == null;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + (age != null ? age.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Student1(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public Student1() {
    }
}